package com.cswnode.Controller;

import com.cswnode.Annotations.LoginJudgment;
import com.cswnode.Entity.Event;
import com.cswnode.Entity.Page;
import com.cswnode.Entity.User;
import com.cswnode.Event.EventProducer;
import com.cswnode.Service.FollowerService;
import com.cswnode.Service.UserService;
import com.cswnode.Util.CommunityUtil;
import com.cswnode.Util.Constant;
import com.cswnode.Util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author csw
 * @create 2020/10/4 1:31
 */
@Controller
public class FollowerController {
    @Autowired
    private HostHolder hostHolder;
    @Autowired
    private FollowerService followerService;
    @Autowired
    private UserService userService;
    @Autowired
    private EventProducer eventProducer;

    @PostMapping("/follow")
    @LoginJudgment
    @ResponseBody
    public String follow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followerService.follow(user.getId(),entityType,entityId);
        //触发关注事件
        Event event = new Event()
                .setTopic(Constant.TOPIC_FOLLOW)
                .setUserId(user.getId())
                .setEntityId(entityId)
                .setEntityType(entityType)
                .setEntityUserId(entityId);
        eventProducer.fireEvent(event);

        return CommunityUtil.getJsonString(0,null);
    }

    @PostMapping("/unfollow")
    @LoginJudgment
    @ResponseBody
    public String unfollow(int entityType,int entityId){
        User user = hostHolder.getUser();
        followerService.unFollow(user.getId(),entityType,entityId);
        return CommunityUtil.getJsonString(0,null);
    }

    private boolean hasFollowStatus(int userId){
        if (hostHolder.getUser() == null){
            return false;
        }
        return followerService.findFollowerStatus(hostHolder.getUser().getId(),userId,Constant.ENTITY_TYPE_USER);
    }

    @GetMapping("/toFollowPage/{userId}")
    @LoginJudgment
    public String toFollowPage(@PathVariable("userId")int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if (user == null){
            throw new RuntimeException("该用户不存在！");
        }
        model.addAttribute("user",user);
        page.setPath("/toFollowPage/"+userId);
        page.setLimit(5);
        page.setRows((int)followerService.findFollowerCount(userId, Constant.ENTITY_TYPE_USER));
        List<Map<String, Object>> followerList = followerService.findFollowerList
                (userId, page.getOffset(), page.getLimit());
       if (followerList != null){
           for (Map<String, Object> map : followerList) {
               User FollowUser = (User) map.get("user");
               map.put("hasFollowStatus",hasFollowStatus(FollowUser.getId()));
           }
       }
       model.addAttribute("users",followerList);
        return "/site/followee";
    }

    @GetMapping("/toFansPage/{userId}")
    @LoginJudgment
    public String toFansPage(@PathVariable("userId")int userId, Page page, Model model){
        User user = userService.findUserById(userId);
        if (user == null){
            throw new RuntimeException("该用户不存在！");
        }
        model.addAttribute("user",user);
        page.setPath("/toFansPage/"+userId);
        page.setLimit(5);
        page.setRows((int)followerService.findFansCount(userId, Constant.ENTITY_TYPE_USER));
        List<Map<String, Object>> fansList = followerService.findFansList
                (userId, page.getOffset(), page.getLimit());
        if (fansList != null){
            for (Map<String, Object> map : fansList) {
                User FollowUser = (User) map.get("user");
                map.put("hasFansStatus",hasFollowStatus(FollowUser.getId()));
            }
        }
        model.addAttribute("users",fansList);
        return "/site/follower";
    }



}
